package org.walker.iotScheduling.modules.iot.v2.strategy.impl;

import org.springframework.scheduling.support.CronTrigger;
import org.walker.iotScheduling.modules.iot.task.ScheduledTask;
import org.walker.iotScheduling.modules.iot.v2.strategy.TimeStrategy;

/**
 * 定义通用的定时任务调度策略类，支持不同的定时策略
 * @author :zhuangguojin
 * @since :2024/12/4 16:18
 */
public class FixedRateSchedulingStrategy implements TimeStrategy {

    private final ScheduledTask task;
    private final String cronExpression;

    // 注入任务和cron表达式
    public FixedRateSchedulingStrategy(ScheduledTask task, String cronExpression) {
        this.task = task;
        this.cronExpression = cronExpression;
    }


    /**
     * 调度任务
     */
    @Override
    public void scheduleTask() {
        task.execute();
    }

    /**
     * 任务策略
     * @return 任务策略
     */
    @Override
    public CronTrigger getCronTrigger() {
        return new CronTrigger(cronExpression);
    }
}
